<?php
include_once('db.php');
include_once('fk_service.php');

//定义常量：分页条数
define('PAGE_PER_NO',10);

//查询出所有的用户信息
function getAllStudent(){
	return getAll("_student");
}

//通过id查询出student信息
function getStudnetById($id){
	$sql = "SELECT * FROM _student WHERE id like '$id'";

	$result = queryStr($sql);

	if($result['num_rows'] != 0){
		return $result['rows'][0];
	}

	return '';
}


function deleteStudentById($id){
	$sql = "DELETE FROM _student WHERE id LIKE '$id';";

	return insert($sql);
}

//=============================dao=======================

//add_student，将学生信息录入到数据库
function add_student(){
	//设置时区,时间需转换为时间戳
	date_default_timezone_set("PRC");
	//不允许空
	$id = $_POST['id'];
	$name = $_POST['name'];

	$department = $_POST['department'];
	$major = $_POST['major'];
	$class = $_POST['class'];
	$personId = $_POST['personId'];
	$enrolTime = $_POST['enrolTime'];
	$birthday = $_POST['birthday'];
	$sex = $_POST['sex'];
	$party = $_POST['party'];
	$province = $_POST['province'];
	$race = $_POST['race'];
	$phone = $_POST['phone'];
	$address = $_POST['address'];
	$email = $_POST['email'];
	$familyTel = $_POST['familyTel'];
	$eduSystme = $_POST['eduSystme'];
	$remark = $_POST['remark'];
	$dormNo = $_POST['dormNo'];
	$birthplace = $_POST['birthplace'];
	
	$ary = array();

	$sql = "INSERT INTO _student (id, name, sex, party, class_id, department_id, major_id, address, birthday, birthplace, dormNo, eduSystme, email, enrolTime, familyTel, personId, phone, province, race, remark) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	checkEmpty($id, $ary, '学号不能为空');
	checkEmpty($name, $ary, '姓名不能为空');
	checkEmpty($sex, $ary, '');
	checkEmpty($party, $ary, '');
	checkEmpty($class, $ary, '');
	checkEmpty($department, $ary, '');
	checkEmpty($major, $ary, '');
	checkEmpty($address, $ary, '');
	checkEmpty(strtotime($birthday), $ary, '');
	checkEmpty($birthplace, $ary, '');
	checkEmpty($dormNo, $ary, '');
	checkEmpty($eduSystme, $ary, '');
	checkEmpty($email, $ary, '');
	checkEmpty(strtotime($enrolTime), $ary, '');
	checkEmpty($familyTel, $ary, '');
	checkEmpty($personId, $ary, '');
	checkEmpty($phone, $ary, '');
	//checkEmpty($photo, $ary, '');
	//checkEmpty($position, $ary, '');
	checkEmpty($province, $ary, '');
	checkEmpty($race, $ary, '');
	checkEmpty($remark, $ary, '');

	$result = prepareInsert($sql, $ary);

	if($result == true){
		echo "<script>alert('创建成功');</script>";
	}else{
		echo "<script>alert('创建失败');</script>";
	}

}


//更新学生信息
function update_student(){
	//设置时区,时间需转换为时间戳
	date_default_timezone_set("PRC");
	//不允许空
	$id = $_POST['id'];
	$name = $_POST['name'];

	$department = $_POST['department'];
	$major = $_POST['major'];
	$class = $_POST['class'];
	$personId = $_POST['personId'];
	$enrolTime = $_POST['enrolTime'];
	$birthday = $_POST['birthday'];
	$sex = $_POST['sex'];
	$party = $_POST['party'];
	$province = $_POST['province'];
	$race = $_POST['race'];
	$phone = $_POST['phone'];
	$address = $_POST['address'];
	$email = $_POST['email'];
	$familyTel = $_POST['familyTel'];
	$eduSystme = $_POST['eduSystme'];
	$remark = $_POST['remark'];
	$dormNo = $_POST['dormNo'];
	$birthplace = $_POST['birthplace'];
	
	$ary = array();

	$sql = "UPDATE _student SET name=?, sex=?, party=?, class_id=?, department_id=?, major_id=?, address=?, birthday=?, birthplace=?, dormNo=?, eduSystme=?, email=?, enrolTime=?, familyTel=?, personId=?, phone=?, province=?, race=?, remark=? WHERE id=?";

	checkEmpty($name, $ary, '姓名不能为空');
	checkEmpty($sex, $ary, '');
	checkEmpty($party, $ary, '');
	checkEmpty($class, $ary, '');
	checkEmpty($department, $ary, '');
	checkEmpty($major, $ary, '');
	checkEmpty($address, $ary, '');
	checkEmpty(strtotime($birthday), $ary, '');
	checkEmpty($birthplace, $ary, '');
	checkEmpty($dormNo, $ary, '');
	checkEmpty($eduSystme, $ary, '');
	checkEmpty($email, $ary, '');
	checkEmpty(strtotime($enrolTime), $ary, '');
	checkEmpty($familyTel, $ary, '');
	checkEmpty($personId, $ary, '');
	checkEmpty($phone, $ary, '');
	//checkEmpty($photo, $ary, '');
	//checkEmpty($position, $ary, '');
	checkEmpty($province, $ary, '');
	checkEmpty($race, $ary, '');
	checkEmpty($remark, $ary, '');
	checkEmpty($id, $ary, '学号不能为空');

	$result = prepareInsert($sql, $ary);

	if($result == true){
		echo "<script>alert('更新成功');</script>";
		echo "<script>history.go(-2);</script>";
	}else{
		echo "<script>alert('更新失败');</script>";
		echo "<script>history.go(-1);</script>";
	}

}


//辅助函数，用于检查变量是否为空，并赋值给一个数组，可以返回一个返回信息（可选）
function checkEmpty($val, &$ary, $result_info = ''){
	if($result_info != ''){
		if($val == ''){
			echo "<script>alert('$result_info');</script>";;
			echo "<script>history.go(-1);</script>";
			exit;
		}
	}

	$ary[] = $val;
}


//add_student end


//delete_student,返回成功条数
function delete_student(){
	$id_ary = $_POST['id_ary'];
	$id_ary = explode(',', $id_ary);
	
	$sucess = 0;
	if(count($id_ary) != 0){
		foreach($id_ary as $id){
			$result = deleteStudentById($id);
			
			if($result == true){
				$sucess++;
			}
		}
	}

	echo $sucess;
}
//delete_student end










//分页，并实现列出所有的学生信息,构建适合的html语句并返回
function pagination(){
	//判断页码
	if(isset($_POST['pageId']) && !empty($_POST['pageId'])){
		$id=(int)$_POST['pageId'];
	}else{
		$id = 0;
	}
	
	$pageLimit = $id * PAGE_PER_NO;
	
	$sql = "select * from _student order by id limit $pageLimit, ".PAGE_PER_NO;
	
	$result = queryStr($sql);
	
	if($result['num_rows'] != 0){
		//构建html语句
		$html = '';
		
		foreach($result['rows'] as $r){
			$id = $r['id'];
			$name = $r['name'];
			$phone = $r['phone'];
			$position = getPositionNameById($r['position']);
			

			$html .= "<tr>";
			$html .= "<td><input type='checkbox' name='checked' value='$id'></td>";

			$html .= "<td>$id</td>";
			$html .= "<td>$name</td>";
			$html .= "<td>$phone</td>";
			$html .= "<td>$position</td>";

			$html .= "<td><a href='action.php?page=search_student&id=$id'>查看</a></td>";

			$html .= "</tr>";
		}

		echo $html;
	}else{
		echo "";
	}
}

//获取分页页码
function getPage(){
	$sql = "select id from _student";
	
	$result = queryStr($sql);

	$result_ary = array(PAGE_PER_NO, $result['num_rows']);
	
	echo json_encode($result_ary);
}







$act = @$_REQUEST['act'];

switch($act){
	case 'pagination':
		pagination();
		break;
	case 'getPage':
		getPage();
		break;
	case 'add_student':
		add_student();
		break;
	case 'delete_student':
		delete_student();
		break;
	case 'update_student':
		update_student();
		break;
}

?>